import { subscribeWithSelector } from 'zustand/middleware'
import create from 'zustand'
import { SimpleUserModel } from '../interface'
import { listToggle } from '@/utils/listSlice'

type StateStore = {
	list: SimpleUserModel[]
	_start: string | null
	_changes: string[]
}

type IStore = StateStore & {
	update: (d: Partial<StateStore>) => void
	reset: () => void
	itemHandle: (uuid: string, type: 'click' | 'ctrl' | 'shift') => void
}

const initialState: StateStore = {
	list: [],
	_start: null,
	_changes: [],
}

export const useUserInteractiveStore = create<IStore>()(
	subscribeWithSelector((set, get) => ({
		...initialState,
		update: (d) => set({ ...d }),
		reset: () => set(initialState),
		itemHandle: (uuid, type) => {
			const { list, _start, _changes } = get()
			const result = listToggle(
				list,
				{
					_start,
					_changes,
					action: type,
					path: 'uuid',
				},
				uuid
			)
			set({ list: result.list, _start: result._start, _changes: result._changes })
		},
	}))
)
